package team.seekers.hula.teamwork.todo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import team.seekers.hula.common.annotation.ApiResponseBody;
import team.seekers.hula.auth.service.MindUtils;
import team.seekers.hula.teamwork.team.team.entity.dao.TeamDO;
import team.seekers.hula.teamwork.team.team.entity.vo.TeamListVO;
import team.seekers.hula.teamwork.team.team.service.TeamService;
import team.seekers.hula.teamwork.todo.entity.dto.ClassAddVO;
import team.seekers.hula.user.user.entity.dao.UserDO;
import team.seekers.hula.user.user.service.UserService;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 课表 前端控制器
 * </p>
 *
 * @author hbc
 * @since 2021-09-03
 */

@Controller
@ApiResponseBody
@RequestMapping("/courses")
public class CoursesController {

    @Autowired
    private UserService userService;
    @Autowired
    private TeamService teamService;


    @ApiOperation(value = "渲染所有课程")
    @GetMapping("/list")
    public JSONObject todayList() {
        String userId = MindUtils.getUserId();
        UserDO user = userService.getById(userId);
        JSONObject jsonObject = JSON.parseObject(user.getClassTable());
        return jsonObject;
    }

    @ApiOperation(value = "渲染所有团队课程")
    @GetMapping("/list-team")
    public JSONObject listTeam(@RequestParam("teamId") String teamId) {
        TeamDO teamDO = teamService.getById(teamId);
        if(teamDO == null){
            //return R.fail();
        }
        JSONObject jsonObject = JSON.parseObject(teamDO.getCourses());
        return jsonObject;
    }

    @PostMapping("/add-team")
    public String add(@RequestBody ClassAddVO classAddVO, @RequestParam("teamId") String teamId){
        TeamDO teamDO = teamService.getById(teamId);
        if(teamDO == null){
            //return R.fail();
        }
        JSONObject jsonObject = JSON.parseObject(teamDO.getCourses());
        List<List<List<Map<String,Object>>>> classTable = (List<List<List<Map<String,Object>>>>) jsonObject.get("classTable");
        List<List<Map<String,Object>>> dayClass = classTable.get(classAddVO.getWeekNumber());

        for (List<Map<String, Object>> aClass : dayClass) {
            if(((Integer)aClass.get(0).get("start")<=classAddVO.getStart() && (Integer)aClass.get(0).get("start")+(Integer)aClass.get(0).get("number")>classAddVO.getStart()) ||
                    (Integer)aClass.get(0).get("start")>=classAddVO.getStart() && (Integer)aClass.get(0).get("start")+(Integer)aClass.get(0).get("number")<=classAddVO.getStart()+classAddVO.getNumber()){
                System.out.println(Arrays.asList((List<Integer>)aClass.get(0).get("week")));
                for (int i = 0;i<classAddVO.getWeek().size();i++) {
                    if(((List<Integer>)aClass.get(0).get("week")).get(i) == 0 && classAddVO.getWeek().get(i) == 1){
                        return "当前时间已有课程！";
                    }
                }
            }
        }

        // 先把课程放进去
        Map<String, Object> addClass = new HashMap<>();
        addClass.put("name",classAddVO.getName());
        addClass.put("teacher",classAddVO.getTeacher());
        addClass.put("number",classAddVO.getNumber());
        addClass.put("place",classAddVO.getPlace());
        addClass.put("start",classAddVO.getStart());
        addClass.put("week",classAddVO.getWeek());

        classTable.get(classAddVO.getWeekNumber()).get(classAddVO.getStart()).add(addClass);

        // 该置0的置0
        for(int i = classAddVO.getStart();i<classAddVO.getStart()+classAddVO.getNumber();i++){
            List<Map<String, Object>> maps = dayClass.get(i);
            List<Integer> week = (List<Integer>) (maps.get(0).get("week"));
            for (int j = 0;j<week.size();j++){
                if(classAddVO.getWeek().get(j) == 1){
                    ((List<Integer>) (maps.get(0).get("week"))).set(j,0);
                }
            }
        }

        Map<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("classTable",classTable);

        Object o = JSON.toJSON(stringObjectHashMap);
        teamDO.setCourses(o.toString());
        teamService.updateById(teamDO);
        return "";
    }



    @PostMapping("/add")
    public String add(@RequestBody ClassAddVO classAddVO){
        String userId = MindUtils.getUserId();
        UserDO user = userService.getById(userId);
        JSONObject jsonObject = JSON.parseObject(user.getClassTable());
        List<List<List<Map<String,Object>>>> classTable = (List<List<List<Map<String,Object>>>>) jsonObject.get("classTable");
        List<List<Map<String,Object>>> dayClass = classTable.get(classAddVO.getWeekNumber());

        for (List<Map<String, Object>> aClass : dayClass) {
            if(((Integer)aClass.get(0).get("start")<=classAddVO.getStart() && (Integer)aClass.get(0).get("start")+(Integer)aClass.get(0).get("number")>classAddVO.getStart()) ||
                    (Integer)aClass.get(0).get("start")>=classAddVO.getStart() && (Integer)aClass.get(0).get("start")+(Integer)aClass.get(0).get("number")<=classAddVO.getStart()+classAddVO.getNumber()){
                System.out.println(Arrays.asList((List<Integer>)aClass.get(0).get("week")));
                for (int i = 0;i<classAddVO.getWeek().size();i++) {
                    if(((List<Integer>)aClass.get(0).get("week")).get(i) == 0 && classAddVO.getWeek().get(i) == 1){
                        return "当前时间已有课程！";
                    }
                }
            }
        }

        // 先把课程放进去
        Map<String, Object> addClass = new HashMap<>();
        addClass.put("name",classAddVO.getName());
        addClass.put("teacher",classAddVO.getTeacher());
        addClass.put("number",classAddVO.getNumber());
        addClass.put("place",classAddVO.getPlace());
        addClass.put("start",classAddVO.getStart());
        addClass.put("week",classAddVO.getWeek());

        classTable.get(classAddVO.getWeekNumber()).get(classAddVO.getStart()).add(addClass);

        // 该置0的置0
        for(int i = classAddVO.getStart();i<classAddVO.getStart()+classAddVO.getNumber();i++){
            List<Map<String, Object>> maps = dayClass.get(i);
            List<Integer> week = (List<Integer>) (maps.get(0).get("week"));
            for (int j = 0;j<week.size();j++){
                if(classAddVO.getWeek().get(j) == 1){
                    ((List<Integer>) (maps.get(0).get("week"))).set(j,0);
                }
            }
        }

        Map<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("classTable",classTable);

        Object o = JSON.toJSON(stringObjectHashMap);
        user.setClassTable(o.toString());
        userService.updateById(user);
        return "";
    }

    @PostMapping("/delete")
    public void delete(@RequestBody ClassAddVO classAddVO){
        String userId = MindUtils.getUserId();
        UserDO user = userService.getById(userId);
        JSONObject jsonObject = JSON.parseObject(user.getClassTable());
        List<List<List<Map<String,Object>>>> classTable = (List<List<List<Map<String,Object>>>>) jsonObject.get("classTable");
        List<List<Map<String,Object>>> dayClass = classTable.get(classAddVO.getWeekNumber());

        // 先把课程删了
        Map<String, Object> addClass = new HashMap<>();
        addClass.put("name",classAddVO.getName());
        addClass.put("teacher",classAddVO.getTeacher());
        addClass.put("number",classAddVO.getNumber());
        addClass.put("place",classAddVO.getPlace());
        addClass.put("start",classAddVO.getStart());
        addClass.put("week",classAddVO.getWeek());

        for (Map<String, Object> stringObjectMap : classTable.get(classAddVO.getWeekNumber()).get(classAddVO.getStart())) {
            if(addClass.equals(stringObjectMap)){
                classTable.get(classAddVO.getWeekNumber()).get(classAddVO.getStart()).remove(stringObjectMap);
                break;
            }
        }

        // 该置1的置1
        for(int i = classAddVO.getStart();i<classAddVO.getStart()+classAddVO.getNumber();i++){
            List<Map<String, Object>> maps = dayClass.get(i);
            List<Integer> week = (List<Integer>) (maps.get(0).get("week"));
            for (int j = 0;j<week.size();j++){
                if(classAddVO.getWeek().get(j) == 1){
                    ((List<Integer>) (maps.get(0).get("week"))).set(j,1);
                }
            }
        }

        Map<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("classTable",classTable);

        Object o = JSON.toJSON(stringObjectHashMap);
        user.setClassTable(o.toString());
        userService.updateById(user);
    }


    @PostMapping("/delete-team")
    public void deleteTeam(@RequestBody ClassAddVO classAddVO,@RequestParam("teamId") String teamId){
        TeamDO teamDO = teamService.getById(teamId);
        if(teamDO == null){
            //return R.fail();
        }
        JSONObject jsonObject = JSON.parseObject(teamDO.getCourses());
        List<List<List<Map<String,Object>>>> classTable = (List<List<List<Map<String,Object>>>>) jsonObject.get("classTable");
        List<List<Map<String,Object>>> dayClass = classTable.get(classAddVO.getWeekNumber());

        // 先把课程删了
        Map<String, Object> addClass = new HashMap<>();
        addClass.put("name",classAddVO.getName());
        addClass.put("teacher",classAddVO.getTeacher());
        addClass.put("number",classAddVO.getNumber());
        addClass.put("place",classAddVO.getPlace());
        addClass.put("start",classAddVO.getStart());
        addClass.put("week",classAddVO.getWeek());

        for (Map<String, Object> stringObjectMap : classTable.get(classAddVO.getWeekNumber()).get(classAddVO.getStart())) {
            if(addClass.equals(stringObjectMap)){
                classTable.get(classAddVO.getWeekNumber()).get(classAddVO.getStart()).remove(stringObjectMap);
                break;
            }
        }

        // 该置1的置1
        for(int i = classAddVO.getStart();i<classAddVO.getStart()+classAddVO.getNumber();i++){
            List<Map<String, Object>> maps = dayClass.get(i);
            List<Integer> week = (List<Integer>) (maps.get(0).get("week"));
            for (int j = 0;j<week.size();j++){
                if(classAddVO.getWeek().get(j) == 1){
                    ((List<Integer>) (maps.get(0).get("week"))).set(j,1);
                }
            }
        }

        Map<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("classTable",classTable);

        Object o = JSON.toJSON(stringObjectHashMap);
        teamDO.setCourses(o.toString());
        teamService.updateById(teamDO);
    }


    @ApiOperation(value = "拉取团队课表")
    @GetMapping("/getTeam")
    public String getTeam() {
        List<TeamListVO> teamListVOS = teamService.listMeIn();
        for (TeamListVO teamListVO : teamListVOS) {
            if(teamListVO.getCourses() != null){
                UserDO user = userService.getById(MindUtils.getUserId());
                user.setClassTable(teamListVO.getCourses());
                userService.updateById(user);
                return "";
            }
        }
        return "您暂时没有加入有课表的团队~";
    }

}
